package com.hwadee.property.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hwadee.property.pojo.ParkingUse;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 车位使用表映射接口
 */
@Repository
@Mapper
public interface ParkingUseMapper extends BaseMapper<ParkingUse> {

    // 计算总记录数
    @Select("<script>" +
            "SELECT COUNT(*) FROM parking_use " +
            "<where> " +
            "   <if test='userId != null'> AND user_id = #{userId} </if> " +
            "   <if test='carNumber != null'> AND car_number = #{carNumber} </if> " +
            "   <if test='parkingId != null'> AND parking_id = #{parkingId} </if> " +
            "   <if test='useType != null and useType.trim() != \"\"'> " +
            "       AND use_type LIKE CONCAT('%', #{useType}, '%') " +
            "   </if> " +
            "   <if test='startTime != null'> AND start_time <![CDATA[ >= ]]> #{startTime} </if> " +
            "   <if test='endTime != null'> AND end_time <![CDATA[ <= ]]> #{endTime} </if> " +
            "</where>" +
            "</script>")
    int countParkingUses(@Param("userId") Long userId,
                         @Param("carNumber") String carNumber,
                         @Param("parkingId") Long parkingId,
                         @Param("useType") String useType,
                         @Param("startTime") Date startTime,
                         @Param("endTime") Date endTime);

    // 分页查询
    @Select("<script>" +
            "SELECT * FROM parking_use " +
            "<where> " +
            "   <if test='userId != null'> AND user_id = #{userId} </if> " +
            "   <if test='carNumber != null'> AND car_number = #{carNumber} </if> " +
            "   <if test='parkingId != null'> AND parking_id = #{parkingId} </if> " +
            "   <if test='useType != null and useType.trim() != \"\"'> " +
            "       AND use_type LIKE CONCAT('%', #{useType}, '%') " +
            "   </if> " +
            "   <if test='startTime != null'> AND start_time <![CDATA[ >= ]]> #{startTime} </if> " +
            "   <if test='endTime != null'> AND end_time <![CDATA[ <= ]]> #{endTime} </if> " +
            "</where> " +
            "ORDER BY create_time ASC " +  // 默认按照创建时间倒序
            "LIMIT #{size} OFFSET #{offset}" +
            "</script>")
    List<ParkingUse> getParkingUses(@Param("userId") Long userId,
                                    @Param("carNumber") String carNumber,
                                    @Param("parkingId") Long parkingId,
                                    @Param("useType") String useType,
                                    @Param("startTime") Date startTime,
                                    @Param("endTime") Date endTime,
                                    @Param("size") int size,
                                    @Param("offset") int offset);

}
